package com.diary.database;

import com.diary.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ChangeDiaryLife {

    //将日记的状态改为完成
    public static int set_finish(String username, String id) {
        int re = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String tablename = username;

        try {
            connection = DBUtils.getConnection();

            String sql = "UPDATE `" + username + "` SET life=1 WHERE id=?;";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1,id);

            re = preparedStatement.executeUpdate();
            // 这里由于是进行新建数据表，不会影响到任何行，所以返回的内容总是false，不能使用这个返回值



        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(resultSet,preparedStatement, connection);
        }

        return re;
    }



    //将日记的状态改为未完成
    public static int set_writing(String username, String id) {
        int re = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String tablename = username;

        try {
            connection = DBUtils.getConnection();

            String sql = "UPDATE `" + username + "` SET life=0 WHERE id=?;";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1,id);

            re = preparedStatement.executeUpdate();
            // 这里由于是进行新建数据表，不会影响到任何行，所以返回的内容总是false，不能使用这个返回值



        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(resultSet,preparedStatement, connection);
        }

        return re;
    }


    //将日记的状态改为删除
    public static int set_delete(String username, String id) {

        int re = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String tablename = username;

        try {
            connection = DBUtils.getConnection();

            String sql = "UPDATE `" + username + "` SET life=-1 WHERE id=?;";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1,id);

            re = preparedStatement.executeUpdate();



        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(resultSet,preparedStatement, connection);
        }

        return re;
    }



}
